package com.lh.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.lh.api.dao.ShopCartMapper;
import com.lh.api.service.ShopCartService;
import com.lh.api.vo.PropertyVo;
import com.lh.api.vo.ShopCartVo;
import com.lh.common.entity.admin.ShopCart;
import com.lh.common.enums.IF;
import com.lh.common.utils.PageUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


@Service
public class ShopCartServiceImpl extends ServiceImpl<ShopCartMapper, ShopCart> implements ShopCartService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer addShopCart(Long userId, Long sid, Integer num) {
        ShopCart temp = baseMapper.selectOne(new QueryWrapper<ShopCart>().eq("uid", userId).eq("sid", sid));
        if(temp!=null){
            num = num + temp.getNum();
        }
        ShopCart shopCart = new ShopCart();
        shopCart.setUid(userId);
        shopCart.setSid(sid);
        Date date = new Date();
        shopCart.setCreateTime(date);
        shopCart.setUpdateTime(date);
        shopCart.setNum(num);
        baseMapper.batchSaveOrUpdate(shopCart);
        //返回购物车数量
        return cartNum(userId);
    }

    @Override
    public PageUtils<ShopCartVo> getCartlist(Map<String, Object> param, Integer pageNum, Integer pageSize) {
        if(pageNum != null && pageSize != null){
            PageHelper.startPage(pageNum, pageSize);
        }
        List<ShopCartVo> shopCartVos = this.baseMapper.getCartlist(param);

        //转换属性数据
        for (ShopCartVo temp : shopCartVos){
            if(StringUtils.isEmpty(temp.getProperty())){
                continue;
            }
            JSONArray jsonArray = JSONArray.fromObject(temp.getProperty());
            List<PropertyVo> propertyVoList = new ArrayList<>();
            PropertyVo property = null;
            for(int i=0;i<jsonArray.size();i++){
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                property = new PropertyVo();
                property.setTitle(jsonObject.get("title") == null ? "" : jsonObject.get("title").toString());
                property.setValue(jsonObject.get("value") == null ? "" : jsonObject.get("value").toString());
                propertyVoList.add(property);
            }
            temp.setPropertyList(propertyVoList);
        }

        return new PageUtils<>(shopCartVos);
    }

    @Override
    public void delShopCart(Long userId, String sid) {
        if (StringUtils.isEmpty(sid) || userId == null){
            return;
        }
        List<String> sidList = Arrays.asList(sid.split(","));
        QueryWrapper<ShopCart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid", userId).in("sid", sidList);
        baseMapper.delete(queryWrapper);
    }

    @Override
    public Integer cartNum(Long userId) {
        QueryWrapper<ShopCart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid", userId);
        return baseMapper.selectCount(queryWrapper);
    }
}
